import 'package:flutter/material.dart';

class NeumorphicAppBar extends StatelessWidget implements PreferredSizeWidget {
  final Widget? title;
  final List<Widget>? actions;
  final double height;
  final Color baseColor;
  final double depth;
  final double borderRadius;
  final bool automaticallyImplyLeading;
  final Widget? leading;
  final double leadingWidth;
  final EdgeInsetsGeometry? titlePadding;

  const NeumorphicAppBar({
    Key? key,
    this.title,
    this.actions,
    this.height = kToolbarHeight,
    this.baseColor = const Color(0xFFE0E5EC),
    this.depth = 8.0,
    this.borderRadius = 15.0,
    this.automaticallyImplyLeading = true,
    this.leading,
    this.leadingWidth = 56.0,
    this.titlePadding,
  }) : super(key: key);

  @override
  Size get preferredSize => Size.fromHeight(height);

  @override
  Widget build(BuildContext context) {
    final canPop = Navigator.of(context).canPop();
    final showLeading = automaticallyImplyLeading && (canPop || leading != null);

    return SafeArea(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: NeumorphicContainer(
          height: height - 16, // 减去padding
          color: baseColor,
          depth: depth,
          borderRadius: borderRadius,
          child: Row(
            children: [
              if (showLeading) ...[
                SizedBox(
                  width: leadingWidth,
                  child: leading ??
                      NeumorphicButton(
                        onPressed: () => Navigator.of(context).pop(),
                        child: const Icon(Icons.arrow_back),
                      ),
                ),
              ],
              Expanded(
                child: Padding(
                  padding: titlePadding ?? EdgeInsets.zero,
                  child: title ?? const SizedBox(),
                ),
              ),
              if (actions != null) ...actions!,
              const SizedBox(width: 12),
            ],
          ),
        ),
      ),
    );
  }
}

class NeumorphicContainer extends StatelessWidget {
  final Widget? child;
  final double? width;
  final double? height;
  final Color color;
  final double depth;
  final double borderRadius;

  const NeumorphicContainer({
    Key? key,
    this.child,
    this.width,
    this.height,
    this.color = const Color(0xFFE0E5EC),
    this.depth = 8.0,
    this.borderRadius = 15.0,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width,
      height: height,
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.circular(borderRadius),
        boxShadow: [
          BoxShadow(
            color: Colors.white.withOpacity(0.8),
            offset: Offset(-depth / 2, -depth / 2),
            blurRadius: depth,
            spreadRadius: 1,
          ),
          BoxShadow(
            color: Colors.grey.withOpacity(0.4),
            offset: Offset(depth / 2, depth / 2),
            blurRadius: depth,
            spreadRadius: 1,
          ),
        ],
      ),
      child: child,
    );
  }
}

class NeumorphicButton extends StatelessWidget {
  final Widget child;
  final VoidCallback? onPressed;
  final double depth;
  final double borderRadius;
  final EdgeInsetsGeometry padding;

  const NeumorphicButton({
    Key? key,
    required this.child,
    this.onPressed,
    this.depth = 4.0,
    this.borderRadius = 8.0,
    this.padding = const EdgeInsets.all(8.0),
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPressed,
      child: NeumorphicContainer(
        width: 40,
        height: 40,
        depth: onPressed != null ? depth : 0,
        borderRadius: borderRadius,
        child: Center(
          child: Padding(
            padding: padding,
            child: child,
          ),
        ),
      ),
    );
  }
}